<template>
	<div>
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center">资产总额</uni-th>
				<uni-th align="center" v-for="item in properties" :key="item.propertyName">
					<div style="white-space: nowrap;">{{item.propertyName}}</div>
				</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr>
				<uni-td align="center">{{propertyTotal}}</uni-td>
				<uni-td align="center" v-for="item in properties" :key="item.propertyName">
					<div style="white-space: nowrap;">{{item.count}}</div>
				</uni-td>
			</uni-tr>
		</uni-table>
		<div class="btn-box">
			<u-button size="small" type="primary" @click="saveToday" text="保存今日资产"></u-button>
			<u-button size="small" type="primary" @click="init" text="刷新"></u-button>
		</div>

		<!-- 日资产表格 -->
		<uni-table border stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
			<uni-tr>
				<uni-th align="center">时间</uni-th>
				<uni-th align="center">资产</uni-th>
			</uni-tr>
			<!-- 表格数据行 -->
			<uni-tr v-for="item in dateProperties" :key="item._id">
				<uni-td align="center">
					<div @click="toDetail(item._id)">{{item.date}}</div>
				</uni-td>
				<uni-td align="center">
					<div @click="toDetail(item._id)">{{item.total}}</div>
				</uni-td>
			</uni-tr>
		</uni-table>
	</div>
</template>

<script>
	import {
		callCloudFunction
	} from '@/api/requests.js'
	export default {
		name: 'dateProperties',
		data() {
			return {
				userInfo: {},
				properties: [],
				dateProperties: [],
			}
		},
		computed: {
			propertyTotal() {
				if (!this.properties.length) {
					return 0
				}
				let propertiesTotal = 0
				this.properties.forEach(property => {
					propertiesTotal += property.count
				})
				return propertiesTotal.toFixed(2)
			}
		},
		methods: {
			init() {
				this.getDateProperties()
				this.getPropertiesList()
			},
			getPropertiesList() {
				this.properties = []
				callCloudFunction('getProperties', {
					owner: this.userInfo._id
				}, res => {
					res.result.data.forEach(item => {
						item.text = `${item.propertyName}(${item.count})`
						item.value = item._id
						this.properties.push(item)
					})
				}, err => {
					uni.showModal({
						content: `查询用户资产失败`,
						showCancel: false
					})
				})
			},
			getDateProperties() {
				callCloudFunction('getDateProperties', {
					owner: this.userInfo._id,
				}, res => {
					this.dateProperties = res.result.data.sort((day1, day2) => {
						const time1 = new Date(day1.date).getTime()
						const time2 = new Date(day2.date).getTime()
						return time2 - time1
					})
				}, err => {
					uni.showModal({
						content: `查询失败`,
						showCancel: false
					})
				})
			},
			saveToday() {
				const now = new Date()
				const year = now.getFullYear()
				const month = now.getMonth() + 1
				const date = now.getDate()
				const hour = now.getHours()
				const minute = now.getMinutes()
				const second = now.getSeconds()
				callCloudFunction('addDateProterties', {
					owner: this.userInfo._id,
					date: `${year}-${month}-${date} ${hour}:${minute}:${second}`,
					total: this.propertyTotal,
					properties: this.properties.filter(item => {
						return item.count
					})
				}, res => {
					uni.showModal({
						content: `添加成功`,
						showCancel: false
					})
					this.getDateProperties()
				}, err => {
					uni.showModal({
						content: `添加失败`,
						showCancel: false
					})
				})
			},
			toDetail(_id) {
				uni.navigateTo({
					url: `/pages/datePropertiesDetail/datePropertiesDetail?_id=` + `${_id}`
				});
			}
		},
		mounted() {
			const that = this
			uni.getStorage({
				key: 'userInfo',
				success: function(res) {
					that.userInfo = res.data
					that.init()
				}
			});
		},
	}
</script>

<style scoped>
	.btn-box {
		display: flex;
		justify-content: flex-end;
		margin: 30rpx 0;
	}

	.btn-box>* {
		width: 180rpx;
		margin: 0;
		margin-right: 20rpx;
	}
</style>
